﻿//Write a program to convert decimal numbers to their binary representation.


using System;

public class DecimalToBinary
{
    public static void Main()
    {
        Console.WriteLine("Enter positive decimal number to convert to binary number");
        Console.Write("Range[N >= 0] N = ");
        int number = int.Parse(Console.ReadLine());
        if (number < 0)
        {
            Console.WriteLine("Bad input!!!");
            Console.WriteLine("The number must be in range >= 0!");
            Console.WriteLine("BYE-BYE!!!");
        }
        else
        {
            byte[] result = ConvertToBaseTwo(number);
            Console.Write("Decimal number '{0}' converted to binary is: ", number);
            Print(result);
            Console.WriteLine();
        }
    }

    public static byte[] ConvertToBaseTwo(int number, int divisor = 2)
    {
        int lenght = (number != 0 ? (int)(Math.Log(number, divisor) + 1) : 1);
        byte[] array = new byte[lenght];
        for (int i = array.Length - 1; i >= 0; i--)
        {
            array[i] = (byte)(number % divisor);
            number /= divisor;
        }

        return array;
    }

    public static void Print(byte[] array)
    {
        foreach (var item in array)
        {
            if (item > 9)
            {
                Console.Write((char)(item + 55));
            }
            else
            {
                Console.Write(item);
            }
        }
    }
}